package com.asiainfo.qmc.sonar.core.git;

import com.asiainfo.qmc.sonar.tool.CqmOper;
import com.asiainfo.qmc.sonar.tool.FileUtil;

import java.io.File;
import java.io.ObjectInputStream;
import java.util.List;
import java.util.stream.Collectors;

public class GitObject implements java.io.Serializable, CqmOper {
    private static final long serialVersionUID = 1L;
    private String revId;
    private String author;
    private String email;
    private String log;
    private String time;
    private List<String> branchs;
    private List<GitFileObject> changeList;

    public List<String> getBranchs() {
        return this.branchs;
    }

    public void setBranchs(List<String> branchs) {
        this.branchs = branchs;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getRevId() {
        return this.revId;
    }

    public void setRevId(String revId) {
        this.revId = revId;
    }

    public String getAuthor() {
        return this.author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getLog() {
        return this.log;
    }

    public void setLog(String log) {
        this.log = log;
    }

    public String getTime() {
        return this.time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public List<GitFileObject> getChangeList() {
        return this.changeList;
    }

    public void setChangeList(List<GitFileObject> changeList) {
        this.changeList = changeList;
    }

    public String toString() {
        return


                "GitObject{revId='" + this.revId + '\'' + ", author='" + this.author + '\'' + ", email='" + this.email + '\'' + ", log='" + this.log + '\'' + ", time='" + this.time + '\'' + ", branchs=" + this.branchs + ", changeList=" + this.changeList.toString() + '}';
    }

    public static GitObject build(String fileName) throws Exception {
        File f = new File(fileName);
        if (!f.exists()) {
            throw new Exception("COMMINFO未找到" + fileName);
        }
        Object object = null;
        ObjectInputStream in = new ObjectInputStream(new java.io.FileInputStream(fileName));
        object = in.readObject();
        in.close();
        return (GitObject) object;
    }

    public static void main(String[] args) throws Exception {
        GitObject object = GitObject.build("/Users/hucg/test/java1/new.20181116190609.incre/CMC_1004_10659_Java.20181116190609/sonar.20181115191936.fa5b5a3c1711ef9dc985671c49bbc465929bd05e/COMMITINFO");
        System.out.println(object.toString());
    }

    public void store(String fileName) {
//        System.out.println(this);
        List<GitFileObject> newChanges = this.getChangeList().stream().filter(change -> null != change.getFile()).collect(Collectors.toList());
        this.setChangeList(newChanges);
//        this.getChangeList().stream().forEach(change -> {
//            System.out.println(change.getFile()+" : "+change.getChangeTypeStr());
//        });
//        System.out.println(this.toString());

        FileUtil.writeObject(this, fileName);
    }

    @Override
    public String version() {
        return this.getRevId();
    }

    @Override
    public void move() {

    }
}
